
$(function() {
	
	$("#label_linkman_a").hide();
	
	Tip.supplier();
	
	$('#supplier').blur(function() {		
		var a = $(this).val();
		
		var body = { };
		body.id = a;
		$.getJSON('contact.php?do=Supplier.ajax_id', body, function(json) {
			$("#c_name").val(json.name);
			$("#c_fullname").val(json.fullname);
			$("#c_address").val(json.address);
			$("#c_web").val(json.url);
			
			$("#currency").val(json.currency);
			$("#type").val(json.pay);
			$("#tax").val(json.tax);
		});
		
		var body = {};
		body.q = a;
		
		$.getJSON('contact.php?do=Supplier.ajax_linkman', body, function(json) {			
			Supplier.linkman(json);
		});
	});
		
	$("#label_linkman").hide();
	
	_currency($('#currency').val());
	
	$('#currency').change(function() {
		var s = $(this).val();
		_currency(s);
	});
	
	$('#date').datepicker(_date1_option);
	$('#t1').datepicker(_date1_option);
	$('#t2').datepicker(_date1_option);
	
	_amount();	
			
	Tip.emp('负责人');
	
	$('textarea').css("width", 400);	
});

function _update_currency(currency, exchange) {
	var ret = new Array();
	$.each(data, function(k, v) {
		if (is_null(v.currency) || v.currency == '-') {
			v.currency = currency;
			v.rate = exchange;
		}
		
		ret.push(v);
	});
	
	return ret;
}

function _amount() {
	var amount = 0;
	var weight1 = 0;
	var weight2 = 0;
	var rmb = 0;
	$.each(data, function(k, v) {
		if (v.ok) {
			if (!is_null(v.price) && !is_null(v.quantity)) {				
				aa = fxmul(v.price, v.quantity);
				
				amount = fxadd(amount, aa);
			}
		}		
	});
	
	$("#amount_t").html(amount);
	
	$("#amount").val(amount);
	_update();	
}

function _currency(s) {
	if (s == null || s == '-')　{		
		return false;
	}
	
	var body = { };
	body.currency = s;
	$.post('finance.php?do=Finance.ajax_exchange', body, function(s) {
		exchange = s;
		$('#rate').val(s);
		
		_update();
	});
}

function _update() {
	var a = $("#amount").val();
	var b = $("#currency").find("option:selected").text();

	Currency.upper(a, b, "#label_amount");
	
	_rmb();
} 

function _rmb() {
	var a = $("#amount").val();
	var b = $('#rate').val();
	var rmb = fxmul(a, b);
	
	$('#rmb').val(rmb);	
}
